package org.openkoala.koala.popup;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.core.ui.internal.wizards.MavenImportWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionDelegate;
import org.openkoala.koala.console.ConsoleFactory;
import org.openkoala.koala.exception.KoalaException;
import org.openkoala.koala.mojo.KoalaModuleCreate;
import org.openkoala.koala.util.ProjectQueryUtil;
import org.openkoala.koala.widget.ModuleAdd;
import org.openkoala.koala.widget.Project;
import org.openkoala.koala.wizard.wizardpage.component.AddModuleDialog;

@SuppressWarnings("restriction")
public class AddNewModuleAction extends BaseAction {

	/**
	 * Constructor for Action1.
	 */
	public AddNewModuleAction() {
		super();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		ConsoleFactory.showConsole();
		// 新增模块方式的调用
		String selectedPath = getResource().getLocationURI().getPath();
		try {
			final Project project = ProjectQueryUtil.getProject(selectedPath);
			AddModuleDialog dialog = new AddModuleDialog(shell, SWT.NONE);
			dialog.setProjectPath(selectedPath);
			dialog.setProject(project);
			dialog.open();
			final ModuleAdd moduleAdd = dialog.getModuleAdd();

			if (moduleAdd != null) {
				ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(shell);
				monitorDialog.run(true, true, new IRunnableWithProgress() {
					
					@Override
					public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
						monitor.beginTask("Running create and import project operation!", 2);
						
						try {
							createModule(moduleAdd);
						} catch (Exception e) {
							e.printStackTrace();
						}
						monitor.worked(1);
						
						importProject(project);
						monitor.worked(2);
						
						monitor.done();
					}
				});
			}

		} catch (KoalaException e) {
			e.printStackTrace();
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "ERROR", e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 创建子模块 
	 * @param moduleAdd
	 * @throws Exception
	 */
	private void createModule(ModuleAdd moduleAdd) throws Exception {
		KoalaModuleCreate moduleCreate = new KoalaModuleCreate();
		moduleCreate.createModule(moduleAdd);
	}

	/**
	 * 导入Maven项目 
	 * @param project
	 */
	private void importProject(final Project project) {
		shell.getDisplay().syncExec(new Runnable() {
			public void run() {
				List<String> urls = new ArrayList<String>();
				urls.add(project.getPath() + "/" + project.getAppName());
				WizardDialog dialog = new WizardDialog(shell, new MavenImportWizard(new ProjectImportConfiguration(), urls));
				dialog.showPage(dialog.getCurrentPage());
				dialog.open();
			}
		});
	}
}
